const express = require('express')
const router = express.Router()
const db = require('../modules/db')
const User = db.User
const multer = require('multer')
const fs = require('fs')


router.post('/info', (req, res) => {
    User.find({ account: req.body.userid }, (err, reslut) => {
        if (!err) {
            if (reslut.length == 0) {
                const user = new User(
                    {

                        account: req.body.userid,
                        password: req.body.pwd,
                        email: req.body.email


                    })

                user.save((err) => {
                    if (!err) {
                        res.json({
                            code: 1000,
                            message: '注册成功,即将跳转页面'
                        })
                    }
                })


            } else {
                res.json({
                    code: 1001,
                    message: '账号已存在'
                })

            }
        }
    })
})

router.post('/find', (req, res) => {

    User.findOne({ account: req.body.userid }, (err, result) => {
        if (!err) {

            if (result != null) {
                if (result.password == req.body.pwd) {

                    const expires = new Date()
                    expires.setMonth(expires.getMonth + 1)
                    res.cookie('userid', req.body.userid, { expires })
                    res.json({
                        code: 2000,
                        message: '登录成功,即将跳转页面'
                    })
                } else {
                    res.json({
                        code: 2001,
                        message: '密码不正确,请重新输入'
                    })

                }
            } else {
                res.json({
                    code: 2002,
                    message: '账号不存在,请重新输入'
                })

            }

        }
    })
})

router.get('/forget_password', (req, res) => {

    User.updateOne({
        email: req.query.data.email,
        account: req.query.data.account
    }, { password: req.query.data.newpwd }, (err) => {

        if (!err) {
            res.json({
                code: 3000,
                message: '修改密码成功'
            })



        }

    })
})

router.post('/logout', (req, res) => {
    res.clearCookie('userid')
    res.json({
        code: 4000,
        message: '退出登录成功'
    })
})

router.get('/setText', (req, res) => {
    // console.log(req.query);
    const date = new Date()
    User.updateOne({ account: req.query.userid }, {
        $push: {
            userText: {
                userid: req.query.userid,
                id: req.query.id,
                path: req.query.path,
                text: req.query.text,
                time: date,
                imgUrl: req.query.imgUrl
            }
        }
    }, (err, result) => {
        if (!err) {


            //    console.log(result);

        }

    })
})

router.get('/getText', (req, res) => {

    User.find((err, result) => {
        if (!err) {

            let newArr = []
            result.forEach(item => {
                newArr.push(...item.userText)
            })
            let html = newArr.filter(item => {


                return item.id == req.query.id && item.path == req.query.path
            }).reverse()
            // console.log(html);
            res.json(html)
        }
    })


})


//配置储存上传文件的storage

const storage = multer.diskStorage({
    destination: 'wwwroot/uploads',
    filename: (req, res, callback) => {
        const petname = formatCookie(req.headers.cookie, 'userid')
        callback(null, petname + '.png')
    },



})

const upload = multer({ storage })
router.post('/uploads', upload.single('file'), (req, res) => {
    res.json({
        code: 6000,
        message: '上传图片成功!'
    })
})

router.post('/setMessage', (req, res) => {

    if (req.headers.cookie) {
        User.updateOne({
            account: formatCookie(req.headers.cookie, 'userid')
        }, {
            $addToSet: {
                watched: req.body

            }
        }, (err, result) => {
            if (!err) {
                // console.log(result);
            }
        })

    }


})

router.post('/getMessage', (req, res) => {
    if (req.headers.cookie) {
        User.findOne({
            account: formatCookie(req.headers.cookie, 'userid')
        }, (err, data) => {

            res.json({
                num: data.watched.length, watched: data.watched
            })
        })
    }

})

function formatCookie(cookie, key) {
    if (cookie.indexOf('; ') == -1) {
        const arr = cookie.split('=')
        if (arr[0] == key) {
            return arr[1]
        }
    } else {
        const items = cookie.split('; ')
        for (let index = 0; index < items.length; index++) {
            const element = items[index];
            const arr = element.split('=')
            if (arr[0] == key) {
                return arr[1]
            }
        }
    }
}

module.exports = router